package net.ewon.avatar.agent;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.ewon.avatar.common.pkg.AvatarPackage;
import net.ewon.avatar.common.pkg.AvatarPackageSource;
import net.ewon.avatar.common.pkg.AvatarPackageWrapper;
import net.ewon.avatar.common.pkg.Version;

import org.springframework.web.client.RestTemplate;

public class SpringRestClient{
	 
    public SpringRestClient(RestTemplate restTemplate){
         
         //we need to call the init method
      
         //now we need to add the parameters to send with a GET
         Map<String, String> map = new HashMap<String, String>();
	 map.put("one", "test");
	 map.put("two", "two-test");

         //and now we call the rest server with a GET
//         AvatarPackage result = restTemplate.getForObject(
//                  "http://localhost:8080/AvatarPackageMgmtService/pkg/name/abc.1.1.5/version/1.1.4/",
//                  AvatarPackage.class);
//	       System.out.println("result=" + result.getVersion());
         
         AvatarPackage[] input = {new AvatarPackage("ok", Version.parse("1.4.5"), null, "guangzh", new Date(System.currentTimeMillis())),
        		 new AvatarPackage("ok", Version.parse("1.4.5"), null, "guangzh", new Date(System.currentTimeMillis()))};

         AvatarPackage[] packages = restTemplate.postForObject(
                 "http://localhost:8088/AvatarAgent/agent/missingPackages",
                 input, AvatarPackage[].class); 
         
         System.out.println("missing=" + packages[0].getName());
         System.out.println("missing=" + packages[0].getVersion());
         
//		ResponseEntity<byte[]> data = restTemplate.postForEntity(
//				"http://localhost:8080/AvatarPackageMgmtService/pkg/getfile",
//				 "AvatarPackageMgmt/1.0.0/AvatarPackageMgmt-beans.xml", byte[].class);
//
//		
//		System.out.println("missing=" + new String(data.getBody()));
	 
     List<String> files = new ArrayList<String>();
     files.add("AvatarPackageMgmt/1.0.0/AvatarPackageMgmt-beans.xml");
	 AvatarPackageSource source = new AvatarPackageSource("localhost", 8080);
	 AvatarPackageWrapper wrapper = new AvatarPackageWrapper("ok", Version.parse("1.0.0"), source);
	 String res =  restTemplate.postForObject(
           "http://localhost:8088/AvatarAgent/agent/sync",
           wrapper, String.class); 
	 
	 System.out.println("res=" + res);
//         
    }

    private void _init(){
//        restTemplate = new RestTemplate();
//	//get all Messages converters
//        List<HttpMessageConverter<?>> mc = restTemplate.getMessageConverters();
//        //now we need to call the Jasckon Message Convert and we need to instaciate it
//	MappingJacksonHttpMessageConverter json = new MappingJacksonHttpMessageConverter();
//        
//        //we inform now whart media type the Jackson JSON Message converter must be convert
//	List<MediaType> supportedMediaTypes = new ArrayList<MediaType>();
//        supportedMediaTypes.add(new MediaType("text","javascript"));
//	json.setSupportedMediaTypes(supportedMediaTypes);
//	mc.add(json);
//	//it's tyme to add the Jackson Message Convert to the Spring RestTemplate
//        restTemplate.setMessageConverters(mc);
   }
}